package com.sdx.console.content.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.common.service.BaseDataService;
import com.sdx.console.content.entity.ResColumn;
import com.sdx.console.content.service.ResColumnService;
import com.sdx.common.entity.DictBusiness;
import com.sdx.console.system.service.DictBusinessService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 栏目管理 接口
 */
@RequestMapping("/content/resColumn")
@Controller
public class ResColumnController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(ResColumnController.class);
    private static final String MODEL = "/content/resColumn";


    @Resource
	private ResColumnService resColumnService;

	@Resource
	private DictBusinessService dictBusinessService;

	@Resource
	private BaseDataService baseDataService;

	/**
	 * 初始化编辑页面
	 *
	 * @param view
	 */
	private void initEditPage(ModelAndView view) {
		List<DictBusiness> moduleList = dictBusinessService.getModule();
		view.addObject("moduleList",moduleList);
		view.addObject("provinceList",baseDataService.getAllProvince());
		//resTagList
	}
    /* 页面 */

    @GetMapping("/index.do")
    @Function("content.resColumn.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        initEditPage(view);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("content.resColumn.edit")
    @ResponseBody
    public ModelAndView edit(Integer colId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        ResColumn resColumn = resColumnService.queryById(colId);
	    initEditPage(view);
        view.addObject("resColumn", resColumn);
		try {
			view.addObject("resColumnJson", JacksonUtils.toJsonString(resColumn,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("content.resColumn.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
	    initEditPage(view);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("content.resColumn.query")
    @ResponseBody
    public JsonResult<PageQuery> list(ResColumn condtion)
    {
        PageQuery page = condtion.getPageQuery();
        resColumnService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,ResColumn resColumn,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();

		CoreUser user = getLoginUser(request);

    	if (isNew) {
			resColumn.setCreatedBy(user.getId().intValue());
			resColumn.setCreatedName(user.getName());
			resColumn.setCreatedTime(new Date());
		    resColumn.setUpdatedBy(user.getId().intValue());
		    resColumn.setUpdatedName(user.getName());
		    resColumn.setUpdatedTime(new Date());
			resColumnService.save(resColumn);
		} else {
			resColumn.setUpdatedBy(user.getId().intValue());
			resColumn.setUpdatedName(user.getName());
			resColumn.setUpdatedTime(new Date());
			resColumnService.updateTemplate(resColumn);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("content.resColumn.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)ResColumn resColumn,HttpServletRequest request
		)
    {

        return save(true,resColumn,request
		);
    }

    @PostMapping("/update.json")
    @Function("content.resColumn.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ResColumn resColumn,HttpServletRequest request
		) {
		return save(false,resColumn,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("content.resColumn.query")
    @ResponseBody
    public JsonResult<ResColumn>queryInfo(Integer colId) {
        ResColumn resColumn = resColumnService.queryById( colId);
        return  JsonResult.success(resColumn);
    }

    @PostMapping("/delete.json")
    @Function("content.resColumn..del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        resColumnService.batchDelResColumn(idList);
        return JsonResult.success();
    }
    

}
